//index.js
//获取应用实例
const app = getApp()
const Pager = require('components/aotoo/core')

const util = require('../../utils/util.js')

const errorTips = {
  'userName': '请输入用户名',
  'address': '请输入详情地址',
}

Pager({
  data: {
    form: {
      $$id: 'formId',
      data: [
        {
          input: [
            {type: 'span', id: 'ss', value: {title: {title: '金额', itemClass: 'input-item-title'}, body: [{title: '￥1280', itemClass: 'color-fail'},{title: ' ', itemClass: 'icon-question ml10 color-placeholder'}], bodyClass: 'input-item-label', itemClass:'flex-row'},  inputClass: 'label-flex-end'},
            {type: 'text', id: 'ss2', title: '银行账号', value: '没什么大不了，死不就还好', readyonly: true, disabled: true},
            {required: true, type: 'text', id: 'userName', title: '用户名', placeholder: '请输入用户名', bindblur: 'onClick?id=userName'},
            {required: true, type: 'textarea', id: 'address', title: '详情地址', placeholder: '请输入详情地址', bindfocus: 'onClick?type=address', 'auto-height': true, 'disable-default-padding': true},
            {
              name: 'test_radio',
              type: 'radio',
              title: '联动',
              value: '0',
              values: ['0','1'],
              titles: ['选项一', '选择二'],
              bindchange: 'onChange'
            },
            {type: 'text', id: 'aa', title: '被联动', bindblur: 'onBlur', placeholder: '请输入对应的数字'},
            {
              name: 'test_checkbox',
              type: 'checkbox',
              title: '爱好',
              values: ['1','2'],
              titles: ['吃', '喝', '玩', '乐'],
              inputClass: 'disN'
            },
            {
              id: 'test_picker1',
              type: 'picker',
              title: '地区',
              values: [
                [
                  {title: '北京', id: '100'},
                  {title: '上海', id: '101'},
                  {title: '广东', id: '102', select: true},
                ]
              ],
              // itemClass: 'text-right-icon',
              // bindchange: 'pickerAction',
              inputClass: 'no-border disN al-item-center right-icon',
            },
            {type: 'button', id: 'clickShow', value: '展开', tap: 'onClickShow', itemClass: 'ss-btn-text icon-toggle-down-c', inputClass: 'color-info'},
          ],
          itemClass: 'bg-fff content-border'
        },
        {
          desc: [
            { title: '·离店后由为食网开具发票'},
            { title: '·离店后超过180天，无法线上申请开票，请联系客服处理'},
          ],
          itemClass: 'item-size-dot'
        },
        {
          input: { type: 'button', id: 'btn', value: '提交申请', itemClass: 'ss-button btn-default larger', tap: 'onSubmit'},
          itemClass: 'nobg item-btn-wrap mb-large'
        }
      ],
      listClass: 'input-container-default  primary p-default'
    }
  },
  $formInst: null,
  abc: 0,
  aaVal: [],
  aaIdx: 0, 
  onBlur: function (e, params, inst) {
    const _val = params.value
    if (this.aaIdx === 0 && _val != '1111') {
      inst.addWarn('aa', {title:"请输入1111", itemClass: 'eeror'})
    }
    else if (this.aaIdx === 1 && _val != '2222') {
      inst.addWarn('aa', {title:"请输入2222", itemClass: 'eeror'})
    }
    else {
      inst.removeWarn('aa')
    }
  },
  onChange: function (e, params, inst) {
    console.log('=========')
    const type = parseInt(params.value)
    this.aaIdx = type
    const byVal = inst.value('aa').value
    inst.removeWarn('aa')
    this.aaVal[type] ? inst.setValue('aa', this.aaVal[type]) : inst.empty('aa')
    console.log(type)
    switch (type) {
      case 0:
        this.aaVal[1] = byVal
        if (this.aaVal[type] && this.aaVal[type] !== '1111') {
          inst.addWarn('aa', {title:"请输入1111", itemClass: 'eeror'})
        }
        break;
      default:
        this.aaVal[0] = byVal
        if (this.aaVal[type] && this.aaVal[type] !== '2222') {
          inst.addWarn('aa', {title:"请输入2222", itemClass: 'eeror'})
        }
        break;
      }
  },
  onClick(e, params, inst) {
    const id = params.id
    inst.removeWarn(id, '')
  },
  onClickShow(e, params, inst) {
    // inst.addClass('disN')
    this.$formInst.addClass('clickShow', 'disN')

    // this.$formInst.removeClass('test_checkbox', 'disN')
    // this.$formInst.removeClass('city', 'disN')
    
    const _val = ['test_checkbox', 'city']
    _val.forEach(item => {
      this.$formInst.removeClass(item, 'disN')
    })
  },
  onClickShow(e, params, inst) {
    // inst.addClass('disN')
    this.$formInst.addClass('clickShow', 'disN')
    // this.$formInst.removeClass('test_checkbox', 'disN')
    // this.$formInst.removeClass('city', 'disN')
    
    const _val = ['test_checkbox', 'test_picker1']
    _val.forEach(item => {
      this.$formInst.removeClass(item, 'disN')
    })
  },
  onSubmit(e, params, inst) {
    console.log(inst, '=========inst')
    const getFormKeys = Object.keys(inst.value())
    let getFormValues = {}

    getFormKeys.forEach(key =>{
      if (errorTips[key] && !inst.getValue(key)) {
        inst.addWarn(key, {title: errorTips[key], itemClass: 'error'})
        return false
      }
      getFormValues[key] = inst.getValue(key)
    })

    console.log(getFormValues)



    // const that = this
    // if (!inst.value('userName').value) {
    //   inst.addWarn('userName', {title:"请输入", itemClass: 'eeror'})
    //   inst.addWarn('address', {title:"请输入", itemClass: 'eeror'})
    // }
    // else {
    //   if (inst.value('userName').value !== 'lgh') {
    //     inst.addWarn('userName', '请输入正确的用户名')
    //   }
    //   else {
    //     wx.showLoading({
    //       title: '加载中'
    //     })
    //     setTimeout(function () {
    //       wx.hideLoading()
    //       if (that.abc === 0) {
    //         that.abc ++
    //         wx.showToast({
    //           title: '请重新提交',
    //           icon: 'none',
    //           duration: 2000
    //         })   
    //       }
    //       else if (that.abc === 1) {
    //         that.abc ++
    //         wx.showToast({
    //           title: '网络有问题，请重新提交',
    //           icon: 'none',
    //           duration: 2000
    //         })  
    //       }
    //       else {
    //         that.abc = 0
    //         wx.showToast({
    //           title: '成功',
    //           icon: 'success',
    //           duration: 2000
    //         })   
    //       }
                 
    //     }, 1000)
    //   }

    // }
  },
  onShow: function () {},
  onReady: function () {
    this.$formInst = this.getElementsById('formId')
    // this.$formInst.value('test_radio', '2')
    // util.insertScreenAD('adunit-7248d31d9572f228')
  },
  onLoad: function () {},
})
